package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.GetActivityUserinfoBean;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.EditorInformationActivity;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/5/8.
 */

public class EditorInformationP extends BaseP<EditorInformationActivity> {
    public void getActivityUserInfo() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getActivityUserInfoData(requestBody)
                .compose(XApi.<GetActivityUserinfoBean>getApiTransformer())
                .compose(XApi.<GetActivityUserinfoBean>getScheduler())
                .compose(getV().<GetActivityUserinfoBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetActivityUserinfoBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(GetActivityUserinfoBean getActivityUserinfoBean) {
                        if ("0".equals(getActivityUserinfoBean.getError_code())) {
                            getV().getActivityUserinfoSucceed(getActivityUserinfoBean.getData());
                        } else if ("1000".equals(getActivityUserinfoBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getActivityUserinfoFail(getActivityUserinfoBean.getMsg());
                        }
                    }
                });
    }

    public void activityUserSet(String activityId, String realName, String idCard, String phone, String email) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("real_name", realName);
        paramsMap.put("idcard", idCard);
        paramsMap.put("phone", phone);
        paramsMap.put("email", email);
        paramsMap.put("activity_id", activityId);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().activityUserSetData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().activityUserSetSucceed(responseBean.getMsg());
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().activityUserSetFail(responseBean.getMsg());
                        }
                    }
                });

    }

    public void activityUserSet(String activityId) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("activity_id", activityId);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().activityUserSetData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().activityUserSetSucceed(responseBean.getMsg());
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().activityUserSetFail(responseBean.getMsg());
                        }
                    }
                });

    }
}
